

//include ("PlayScene.js");

var res = {
    bg: "img/bg.jpg",
    logo: "img/logo.png",
    horse: "img/h/h4.png",
    horse_logo: "img/horse1.png",
    railing: "img/railing.png",
	horse_plist : "img/h/h.plist",
	horse_plist_png : "img/h/h.png",
    cs_scene : "res/Login.json",

};

var g_resources = [ res.cs_scene, 
                    res.bg, res.logo, 
                    res.horse, 
                    res.horse_logo, 
                    res.railing, 
                    res.horse_plist, 
                    res.horse_plist_png,
                    "res/Login/BaS31.png",
                    "res/Login/BaS32.png",
                    "res/Login/Frd11.png",
                    "res/Login/LI01.png",
                    "res/Login/LI02.png",
                    "res/Login/LI02.png"
                    ];

var horse_gif = ["img/h/h4.png","img/h/h3.png","img/h/h2.png","img/h/h1.png"];

var StartLayer = cc.Layer.extend({
        ctor:function() {
                this._super();
                var size = cc.winSize;
                
                /*********************************************************/
                //添加文字
                var helloLabel = new cc.LabelTTF("草泥马奔腾", "", 35);
                helloLabel.x = size.width / 2;
                helloLabel.y = size.height - 70;
                helloLabel.setColor(cc.color(0,0,255));
                this.addChild(helloLabel, 2);

                var title_bg = new cc.Sprite(res.bt1);
                title_bg.x = size.width / 2;
                title_bg.y = size.height - 120;
                //this.addChild(title_bg, 1);

                /*********************************************************/
                //添加背景图片
                this.bgSprite = new cc.Sprite(res.bg);
                this.bgSprite.attr({
                        x:size.width / 2,
                        y:size.height / 2,
                });
                this.addChild(this.bgSprite, 0); 


                /*********************************************************/
                //添加按钮
                /*
                var startItem = new cc.MenuItemImage(
                        res.bt1,
                        res.bt2,
                        function() {
                                cc.director.pushScene(new PlayScene());
                        }, this);

                startItem.attr({
                        x: size.width / 2,
                        y: size.height / 2 - 20,
                        anchorX: 0.5,
                        anchorY: 0.5,
                });
                */


                console.log(size.width);
                console.log(size.height);


                var StartMenuItem = new cc.MenuItemFont(
                                "Let's Go (Easy)!",
                                function () {
                                        var s = new PlayScene();
                                        s.horse_move_time = 10;
                                        s.horse_count = 5;
                                        cc.director.pushScene(s);
                                }, this);
                StartMenuItem.attr({
                        x: size.width/2,
                        y: size.height / 2  + 100,
                        anchorX: 0.5,
                        anchorY: 0.5,
                });


                var StartMenuItem2 = new cc.MenuItemFont(
                                "Let's Go (Medium)!",
                                function () {
                                        var s = new PlayScene();
                                        s.horse_move_time = 6;
                                        s.horse_count = 7;
                                        cc.director.pushScene(s);
                                }, this);
                StartMenuItem2.attr({
                        x: size.width/2,
                        y: size.height / 2  + 50,
                        anchorX: 0.5,
                        anchorY: 0.5,
                });


                var StartMenuItem3 = new cc.MenuItemFont(
                                "Let's Go (Hard)!",
                                function () {
                                        var s = new PlayScene();
                                        s.horse_move_time = 4;
                                        s.horse_count = 10;
                                        cc.director.pushScene(s);
                                }, this);
                StartMenuItem3.attr({
                        x: size.width/2,
                        y: size.height / 2 ,
                        anchorX: 0.5,
                        anchorY: 0.5,
                });



                var StartMenuItem1 = new cc.MenuItemFont(
                                "Let's Go (Debug)!",
                                function () {
                                        var s = new PlayScene();
                                        s.horse_move_time = 10;
                                        s.horse_count = 5;
                                        s.debug = 1;

                                        cc.director.pushScene(s);
                                }, this);
                StartMenuItem1.attr({
                        x: size.width/2,
                        y: size.height / 2 - 50,
                        anchorX: 0.5,
                        anchorY: 0.5,
                });



                var menu = new cc.Menu([StartMenuItem2, StartMenuItem3, StartMenuItem, StartMenuItem1]);
                menu.x = 0; 
                menu.y = 0; 
                menu.setColor(cc.color(255,0,0));
                this.addChild(menu, 1);


                var logo = new cc.Sprite(res.logo);
                logo.attr({
                        x: size.width - 50,
                        y: 25,
                });
                logo.setScale(0.7);
                this.addChild(logo, 10);

                //add horse logo
                var horse_logo = new cc.Sprite(res.horse_logo);
                horse_logo.attr({
                        x: 0,
                        y: 0,
                        scale : 0.6,
                });
                horse_logo.setRotation(30);  

                horse_logo.is_running = false;
                //add some event
                var horse_touch_listener = cc.EventListener.create({
                        event : cc.EventListener.TOUCH_ONE_BY_ONE,
                        swallowTouches: false,
                        onTouchBegan: function(touch, event){

                                var pos = touch.getLocation();
                                var target = event.getCurrentTarget();

                                if (target.is_running){
                                        return false;
                                }

                                
                                if (!cc.rectContainsPoint(target.getBoundingBox(), pos)) {
                                        return false;
                                }

                                var scale_ac_1 = new cc.ScaleBy(0.2, 1.25, 1.25);
                                var scale_ac_2 = new cc.ScaleBy(0.2, 0.8, 0.8);
                                
                                var func = cc.CallFunc.create(function() {
                                        target.is_running = false;
                                }, target);


                                var seq = cc.Sequence.create([scale_ac_1, scale_ac_2, func]);
                                target.runAction(seq); 
                                target.is_running = true;

                                //添加文字
                                var lable = new cc.LabelTTF("点你妹啊点～！","", 20);
                                lable.attr({
                                        x: 200,
                                        y: 130, 
                                        color: cc.color(255,0,0),
                                        rotation: -10,
                                });
                                target.getParent().addChild(lable);
                                var ac = new cc.FadeOut(1);
                                var seq_1 = cc.Sequence.create(ac, cc.CallFunc.create(function(){
                                        lable.removeFromParent(); 
                                        console.log("remove");
                                }, lable));
                                lable.runAction(seq_1);
                        },
                });
                cc.eventManager.addListener(horse_touch_listener, horse_logo);

                this.addChild(horse_logo, 0); 


                var horse_ac = new cc.MoveTo(0.5, cc.p(60,60));
                horse_logo.runAction(horse_ac);


               
                //读取json数据                
                /*
                var cs_node = ccs.csLoader.createNode(res.cs_scene);
                cs_node.setPosition(size.width / 2, 0);
                cs_node.setScale(0.2);
                this.addChild(cs_node, 100);
                */


                return true;

        }
});


var StartScene = cc.Scene.extend({
        onEnter:function() {
                this._super();
                var layer = new StartLayer();
                this.addChild(layer);
        }
});



function onLoad ()
{
        cc.game.onStart = function()
        {
                //load resources
                cc.LoaderScene.preload(g_resources, function () {
                        cc.director.runScene(new StartScene());
                }, this);

                cc.view.setDesignResolutionSize(320, 480, cc.ResolutionPolicy.SHOW_ALL);
                cc.view.resizeWithBrowserSize(true);
        };
        cc.game.run("gameCanvas");
}

